<template>
<section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
        <el-form :inline="true" :model="filters">
            <el-form-item>
                <el-input v-model="filters.userName" placeholder="姓名"></el-input>
            </el-form-item>
            <el-form-item>
                <el-input v-model="filters.userAccount" placeholder="账号"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" v-on:click="queryUserByPage">查询</el-button>
            </el-form-item>
        </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="users" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="userName" label="姓名" sortable></el-table-column>
        <el-table-column prop="userAccount" label="账号" sortable></el-table-column>
        <el-table-column prop="sex" label="性别" :formatter="formatSex" sortable></el-table-column>
        <el-table-column prop="age" label="年龄" sortable></el-table-column>
        <el-table-column prop="phoneNumber" label="电话号码" sortable></el-table-column>
        <el-table-column prop="email" label="邮箱" sortable></el-table-column>
        <el-table-column prop="userStatus" label="账号状态" :formatter="formatStatus" sortable></el-table-column>
        <el-table-column label="操作" width="200">
            <template slot-scope="scope">
                <el-button type="primary" size="small" @click="handleEdit(scope.$index, scope.row)">详情</el-button>
            </template>
        </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
        <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="size" :total="total" style="float:right;"></el-pagination>
    </el-col>
</section>
</template>

<script>
import {
    mapGetters
} from "vuex";
import * as api from "@/api/api.js";
import {
    parse
} from "path";

export default {
    data() {
        return {
            defaultProps: {
                children: "children",
                label: "nodeName"
            },
            filters: {
                userAccount: "",
                userName: "",
                userType : ""
            },
            showUrl: api.showUrl,
            uploadFileUrl: api.uploadFileUrl,
            users: [],
            total: 0,
            current: 1,
            size: 10,
            listLoading: false,
            sels: [], //列表选中列
            editFormVisible: false, //编辑界面是否显示
            editLoading: false,
            editFormRules: {
                // userName: [{
                //     required: true,
                //     message: "请填写姓名",
                //     trigger: "blur"
                // }]
            },
            //编辑界面数据
            editForm: {},
            userTypes: [
                { value: "", label: "请选择用户类型" },
                { value: "1", label: "医生" },
                { value: "2", label: "护士" },
                { value: "3", label: "病人" },
                { value: "4", label: "后勤" }
            ],
        };
    },
    computed: {
        ...mapGetters("user", ["getUser"])
    },
    methods: {
        formatSex: function (row, column) {
            return row.sex == "1" ? "男" : "女";
        },
        formatStatus: function (row, column) {
            return row.userStatus == "1" ? "正常" : "停用";
        },
        handleCurrentChange(val) {
            this.current = val;
            this.queryUserByPage();
        },
        //获取用户列表
        queryUserByPage() {
            let para = {
                current: this.current,
                size: this.size,
                userAccount: this.filters.userAccount,
                userName: this.filters.userName,
                userType: '3',
                createUser : localStorage.getItem("userId")
            };
            this.listLoading = true;
            api.queryUserByPage(para)
                .then(res => {
                    this.total = res.returnPage.total;
                    this.users = res.returnPage.records;
                    this.listLoading = false;
                })
                .catch(() => {
                    this.listLoading = false;
                });
        },
        //删除
        handleDel: function (index, row) {
            this.$confirm("确认删除该记录吗?", "提示", {
                    type: "warning"
                })
                .then(() => {
                    this.listLoading = true;
                    let para = {
                        userId: row.userId
                    };
                    api.delUser(para).then(res => {
                        this.listLoading = false;
                        if (res.message == 'success') {
                            this.$message({
                                message: "删除成功",
                                type: "success"
                            });
                        }
                        this.queryUserByPage();
                    });
                })
                .catch(() => {
                    this.listLoading = false;
                });
        },
        //显示编辑界面
        handleEdit: function (index, row) {
            localStorage.setItem("patientId",row.userId);
            this.$router.push({
                path: "/HealthInfoPrint"
            })
        },
        //显示新增界面
        handleAdd: function () {
            this.editFormVisible = true;
            this.editForm = {};
        },
        //编辑
        editSubmit: function () {
            this.$refs.editForm.validate(valid => {
                if (valid) {
                    this.$confirm("确认提交吗？", "提示", {}).then(() => {
                        this.editLoading = true;
                        let para = Object.assign({}, this.editForm);
                        para.createUser = this.getUser.userId;

                        api.updateUser(para)
                            .then(res => {
                                this.editLoading = false;
                                if (res.message == 'success') {
                                    this.$message({
                                        message: "提交成功",
                                        type: "success"
                                    });
                                    this.$refs["editForm"].resetFields();
                                    this.editFormVisible = false;
                                    this.queryUserByPage();
                                }
                            })
                            .catch(() => {
                                this.listLoading = false;
                            });
                    });
                }
            });
        },
        handleCheckChange(data, checked, indeterminate) {
            console.log(data, checked, indeterminate);
        },
        selsChange: function (sels) {
            this.sels = sels;
        },
        handleAvatarSuccess(res, file) {
            this.editForm.thingsImg = res.returnObj;
            this.$forceUpdate();
        },
        beforeAvatarUpload(file) {
            const isJPG = file.type === "image/jpeg";
            const isLt2M = file.size / 1024 / 1024 < 2;

            if (!isJPG) {
                this.$message.error("上传图片只能是 JPG 格式!");
            }
            if (!isLt2M) {
                this.$message.error("上传图片大小不能超过 2MB!");
            }
            return isJPG && isLt2M;
        },
    },
    mounted() {
        this.queryUserByPage();
    }
};
</script>

<style scoped>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
}

.avatar-uploader .el-upload:hover {
    border-color: #409eff;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
}

.avatar {
    width: 178px;
    height: 178px;
    display: block;
}

.file {
    position: relative;
    display: inline-block;
    background: #63E0EC;
    border: 1px solid #99D3F5;
    border-radius: 4px;
    padding: 4px 12px;
    overflow: hidden;
    color: #FFFFFF;
    text-decoration: none;
    text-indent: 0;
    line-height: 30px;
    height: 30px;
}

.file input {
    position: absolute;
    font-size: 100px;
    right: 0;
    top: 0;
    opacity: 0;
}

.file:hover {
    background: #AADFFD;
    border-color: #78C3F3;
    color: #004974;
    text-decoration: none;
}
</style>
